/**
 * 2025/01/01 - 2025/12/31
 */
package com.snail.starter.cloud.configuration;

import com.snail.starter.cloud.advice.BasicExceptionAdvice;
import com.snail.starter.cloud.advice.BasicResponseWrapperInitializing;
import com.snail.starter.cloud.health.HealthController;
import com.snail.starter.cloud.mapper.JacksonObjectMapper;
import com.snail.starter.utils.rest.RestTemplateHolder;
import com.snail.starter.utils.spring.SpringBeanContext;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author zgadmin
 * @date 2025/8/26 13:23
 */
@Configuration
@Import({WebMvcAutoConfiguration.class,
        I18nAutoConfiguration.class, TracingAutoConfiguration.class, BasicExceptionAdvice.class,
        BasicResponseWrapperInitializing.class, HealthController.class, RestTemplateHolder.class})
public class CloudAutoConfiguration {

    @Bean
    SpringBeanContext springBeanContext() {
        return new SpringBeanContext();
    }

    @Bean
    JacksonObjectMapper jacksonObjectMapper() {
        return new JacksonObjectMapper();
    }

    @Bean
    public RestTemplate restTemplate() {
        OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(new OkHttpClient().newBuilder()
                .connectionPool(new ConnectionPool(200, 5, TimeUnit.MINUTES))
                .build());
        factory.setConnectTimeout(3000);
        factory.setReadTimeout(10000);
        return new RestTemplate(factory);
    }
}